package ch.myDungeons.game;

import java.io.Serializable;
import java.util.ArrayList;

import ch.myDungeons.gameObjects.Item;
import ch.myDungeons.io.LanguagePack;

public class Inventory implements Serializable{
	private static final long serialVersionUID = -4637761175562422190L;
	
	private ArrayList<BagSlot> bagSlots = new ArrayList<BagSlot>();
	
	/** Adds a single item to the inventory */
	public int addItem(Item item){
		return addItem(item, 1);
	}
	
	
	/** Adds an item to the inventory */
	public int addItem(BagSlot bagSlot){
		return addItem(bagSlot.getItem(), bagSlot.getCount());
	}
	
	/** Adds an item with count to the inventory */
	public int addItem(Item item, int count){
		boolean stacked = false;
		int stackIndex = 0;
		
		for (int j = 0; j < bagSlots.size(); j++) {
			if (item.getClass().equals(bagSlots.get(j).item.getClass()) && item.getName().equals(bagSlots.get(j).item.getName())) { // is same item
				bagSlots.get(j).count += count;
				stacked = true;
				stackIndex = j;
			}
		}
		
		if (!stacked) {
			bagSlots.add(new BagSlot(item, count));
			return bagSlots.size() - 1;
		} else {
			return stackIndex;
		}
	}

	public BagSlot get(int index){
		return bagSlots.get(index);
	}
	
	public int size(){
		return bagSlots.size();
	}

	public boolean isEmpty() {
		return bagSlots.isEmpty();
	}

	public void remove(int i) {
		bagSlots.remove(i);
	}
	
	public static class BagSlot implements Serializable{
		private static final long serialVersionUID = 5601086840062689570L;
		
		Item item;
		int count;
		public BagSlot(Item item, int count) {
			this.item = item;
			this.count = count;
		}
		
		public String getNameWithCount() {
			if (count == 1) {
				return LanguagePack.capitalizeString(item.getName());
			}
			return count + "x " + LanguagePack.capitalizeString(item.getName());
		}
		
		public Item getItem() {
			return item;
		}
		
		public int getCount() {
			return count;
		}
		
		public void setCount(int count) {
			this.count = count;
		}

		public void removeOne() {
			count--;
		}
		public String getSave() {
			return count + "<ITEMINFO>" + item.getSave();
		}
	}
}
